/**
 * @file FSPParsingTest.cpp
 * @author Cosimo Sacco <cosimosacco@gmail.com>
 *
 * @section LICENSE
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 **/

#include <boost/program_options.hpp>
#include <iostream>
#include <fstream>
#include "CommandInterpreter.h"
#include "FSPLanguage4.h"
using namespace std;

int main(int argc, char** argv)
{
  std::string scriptFileDescriptor;
  istream* script = NULL;
  ifstream scriptFile;
  scriptFile.exceptions(std::ifstream::failbit);
  boost::program_options::options_description options("FSP Test options");
  options.add_options()
  ("help,h", "Produce this help message")
  (
    "script,s",
    boost::program_options::value<std::string>
      (
        &scriptFileDescriptor
      )
      ->default_value("stdin"),
    "Specify a script file."
  )
  ;
  boost::program_options::variables_map specifiedOptions;
  try
  {
    boost::program_options::store
      (
        boost::program_options::parse_command_line
          (
            argc, argv, options
          ),
        specifiedOptions
      );
  }
  catch(...)
  {
    std::cout << "The specified options are invalid" << std::endl;
    std::cout << options << std::endl;
    return 1;
  }
  boost::program_options::notify(specifiedOptions);
  if(specifiedOptions.count("help"))
  {
    std::cout << options << std::endl;
    return 0;
  }
  if
    (
      specifiedOptions.count("script")
      &&
      !
      (
        (
          specifiedOptions["script"].as<std::string>()
        )
        .compare("stdin") == 0
      )
    )
  {
    try
    {
      scriptFile.open
        (
          (
            specifiedOptions["script"].as<std::string>()
          ).c_str()
        )
      ;
      script = &scriptFile;
    }
    catch(std::ifstream::failure)
    {
      std::cout
      << specifiedOptions["script"].as<std::string>()
      << ": The specified script does not exist" << std::endl;
      return 1;
    }
  }
  else
  {
    script = &std::cin;
  }
  CommandInterpreter<FSPLanguage> FSPTester("Process algebra madness!", script);
  try
  {
    FSPTester.run();
  }
  catch(std::ifstream::failure){return 0;/*std::cout << "No more input lines?" << std::endl;*/}
  return 0;
}
